package com.spic.business.mq.utils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spic.business.mq.domain.Driverless;
import com.spic.business.mq.domain.FindVehicleByIndexCodeRequest;
import com.spic.business.mq.domain.Hikvehiclealarm;
import com.spic.business.mq.domain.HikvehiclecarInfo;
import com.spic.business.vehicle.domain.VehicleManagement;
import com.spic.business.violation.supervision.speed.domain.ViolationsSpeedRecordVo;
import com.spic.business.violation.supervision.speed.mapper.ViolationsSpeedRecordVoMapper;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class PushCallback implements MqttCallback {
    private static final Logger logger = LoggerFactory.getLogger(MqttPushClient.class);

    @Autowired
    private MqttConfig mqttConfig;

    private static MqttClient client;

    private static String _topic;
    private static String _qos;
    private static String _msg;
    @Autowired
    private ViolationsSpeedRecordVoMapper violationsSpeedRecordVoMapper;


    @Override
    public void connectionLost(Throwable throwable) {
        // 连接丢失后，一般在这里面进行重连
        logger.info("连接断开，可以做重连");
        if (client == null || !client.isConnected()) {
            mqttConfig.getMqttPushClient();
        }
    }

    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        // subscribe后得到的消息会执行到这里面
        logger.info("接收消息主题 : " + topic);
        logger.info("接收消息Qos : " + mqttMessage.getQos());
        logger.info("接收消息内容 : " + new String(mqttMessage.getPayload()));

        _topic = topic;
        _qos = mqttMessage.getQos()+"";
        _msg = new String(mqttMessage.getPayload());

        //处理报警消息入库
        JSONObject responseStr = JSONObject.parseObject(_msg).getJSONObject("params").getJSONArray("events").getJSONObject(0).getJSONObject("data");
        Hikvehiclealarm hikvehiclealarm = responseStr.toJavaObject(Hikvehiclealarm.class);
        //根据车辆ID获取车牌号码 处理告警类型
        FindVehicleByIndexCodeRequest findVehicleByIndexCodeRequest = new FindVehicleByIndexCodeRequest();
        findVehicleByIndexCodeRequest.setIndexCode(hikvehiclealarm.getVehicleIndexCode());
        String carinfo = HikvehiclealarmUtil.findVehicleByIndexCode(findVehicleByIndexCodeRequest);
        JSONObject carobject = JSONObject.parseObject(carinfo).getJSONObject("data");
        HikvehiclecarInfo hikvehiclecarInfo = carobject.toJavaObject(HikvehiclecarInfo.class);

        ViolationsSpeedRecordVo violationsSpeedRecordVo = new ViolationsSpeedRecordVo();
        violationsSpeedRecordVo.setVehicleNumber(hikvehiclecarInfo.getPlateNo().trim());
        violationsSpeedRecordVo.setIllegalTime(hikvehiclealarm.getAlarmTime());
        String ssp = hikvehiclealarm.getSpeed().toString();
        if(StringUtils.isNotEmpty(ssp)){
            double v1 = Double.parseDouble(ssp);
            double sp = v1/100000d;
            violationsSpeedRecordVo.setRunningSpeed(sp);//超速速度
        }
        violationsSpeedRecordVo.setDataType("机动车平台");
        violationsSpeedRecordVo.setCreateBy("1");//创建人
        violationsSpeedRecordVo.setPenaltyAmount(300d);//罚款金额
        violationsSpeedRecordVo.setIllegalArea("1");//矿区
        violationsSpeedRecordVo.setIllegalType("1");//作业类违章

        String atp = hikvehiclealarm.getEventType().toString();
        if(StringUtils.isNotEmpty(atp)){
            if("123405".equals(atp)){ //超速
                violationsSpeedRecordVo.setIllegalSources("0");//超速为0
            } else if("132371".equals(atp)){
                violationsSpeedRecordVo.setIllegalSources("2");//疲劳驾驶为2
            } else if ("132374".equals(atp)){
                violationsSpeedRecordVo.setIllegalSources("3");//接打电话
            } else if ("132375".equals(atp)) {
                violationsSpeedRecordVo.setIllegalSources("4");//抽烟
            } else if ("132373".equals(atp)){
                violationsSpeedRecordVo.setIllegalSources("5");//分神
            } else if ("132377".equals(atp)){
                violationsSpeedRecordVo.setIllegalSources("6");//驾驶员异常
            }
        }
        if(StringUtils.isNotEmpty(violationsSpeedRecordVo.getIllegalSources()))
        {
            violationsSpeedRecordVoMapper.insertViolationsSpeedRecordVo(violationsSpeedRecordVo);
        }


    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        logger.info("deliveryComplete---------" + iMqttDeliveryToken.isComplete());
    }

    //别的Controller层会调用这个方法来  获取  接收到的硬件数据
    public String receive() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("topic", _topic);
        jsonObject.put("qos", _qos);
        jsonObject.put("msg", _msg);
        return jsonObject.toString();
    }

}